package com.geeke.common.permission;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.compress.utils.Lists;

import com.geeke.common.data.Parameter;
import com.geeke.org.entity.Company;
import com.geeke.org.entity.Department;
import com.geeke.org.service.CompanyService;
import com.geeke.org.service.DepartmentService;
import com.geeke.sys.entity.User;
import com.geeke.utils.SessionUtils;
import com.geeke.utils.SpringUtils;

/**
 * 组织结构数据权限
 * @author lys
 * @version 2024-12-24
 */
public class OrgData {
    private static final String  NONE_DATA_RETURN = "null";
    private static final Map<String, Function<String, String>> map;
    static {
        map = new HashMap<>();
        map.put("MyCompany", x -> getMyCompany());          // 所属公司
        map.put("MySubCompany", x -> getMySubCompany());         // 下属公司
        map.put("MyCompanyAndSub", x -> getMyCompanyAndSub());      // 所属公司及下属公司

        map.put("MyDepartment", x -> getMyDepartment());     // 所属部门
        map.put("MySubDepartment", x -> getMySubDepartment());     // 下属部门
        map.put("MyDepartmentAndSub", x -> getMyDepartmentAndSub());     // 所属部门及下属部门
        
        map.put("Department@MyCompany", x -> getDepartmentByMyCompany());    // 所属公司所有部门
        map.put("Department@MySubCompany", x -> getDepartmentByMySubCompany()); // 下属公司所有部门
        map.put("Department@MyCompanyAndSub", x -> getDepartmentByMyCompanyAndSub()); // 所属公司及下属公司所有部门
    }

    public static Function<String, String> get(String val) {
        return map.get(val);
    }
 

    // 获取当前登录用户所属公司id
    private static String getMyCompany() {
        User user = SessionUtils.getUser();
        return user.getCompany().getId();
    }

    private static String getMySubCompany() {
        User user = SessionUtils.getUser();
        Company myCompany = user.getCompany();

        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myCompany.getIds()));
        parameters.add(new Parameter("id", "<>", myCompany.getId()));

        CompanyService companyService = (CompanyService)SpringUtils.getBean("companyService");
        List<Company> companys = companyService.listAll(parameters, null);
        if (companys != null && companys.size() > 0) {
            return companys.stream().map(Company::getId).collect(Collectors.joining(","));
        }
       
        return NONE_DATA_RETURN;
    }

    private static String getMyCompanyAndSub() {
        User user = SessionUtils.getUser();
        Company myCompany = user.getCompany();

        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myCompany.getIds()));
        // parameters.add(new Parameter("id", "<>", myCompany.getId()));

        CompanyService companyService = (CompanyService)SpringUtils.getBean("companyService");
        List<Company> companys = companyService.listAll(parameters, null);
        if (companys != null && companys.size() > 0) {
            return companys.stream().map(Company::getId).collect(Collectors.joining(","));
        }
       
        return NONE_DATA_RETURN;
    }


    // 获取当前登录用户所属部门id
    private static String getMyDepartment() {
        User user = SessionUtils.getUser();
        return user.getDepartment().getId();
    }

    private static String getMySubDepartment() {
        User user = SessionUtils.getUser();
        Department myDepartment = user.getDepartment();

        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myDepartment.getIds()));
        parameters.add(new Parameter("id", "<>", myDepartment.getId()));

        DepartmentService departmentService = (DepartmentService)SpringUtils.getBean("departmentService");
        List<Department> departments = departmentService.listAll(parameters, null);
        if (departments != null && departments.size() > 0) {
            return departments.stream().map(Department::getId).collect(Collectors.joining(","));
        }
       
        return NONE_DATA_RETURN;
    }

    private static String getMyDepartmentAndSub() {
        User user = SessionUtils.getUser();
        Department myDepartment = user.getDepartment();

        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myDepartment.getIds()));
        // parameters.add(new Parameter("id", "<>", myDepartment.getId()));

        DepartmentService departmentService = (DepartmentService)SpringUtils.getBean("departmentService");
        List<Department> departments = departmentService.listAll(parameters, null);
        if (departments != null && departments.size() > 0) {
            return departments.stream().map(Department::getId).collect(Collectors.joining(","));
        }
       
        return NONE_DATA_RETURN;
    }
    
    private static String getDepartmentByMyCompany() {
        User user = SessionUtils.getUser();
        Company myCompany = user.getCompany();

        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("company_id", "=", myCompany.getId()));

        DepartmentService departmentService = (DepartmentService)SpringUtils.getBean("departmentService");
        List<Department> departments = departmentService.listAll(parameters, null);
        if (departments != null && departments.size() > 0) {
            return departments.stream().map(Department::getId).collect(Collectors.joining(","));
        }
        return NONE_DATA_RETURN;
    }


    private static String getDepartmentByMySubCompany() {
        User user = SessionUtils.getUser();
        Company myCompany = user.getCompany();

        // 下级公司
        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myCompany.getIds()));
        parameters.add(new Parameter("id", "<>", myCompany.getId()));

        CompanyService companyService = (CompanyService)SpringUtils.getBean("companyService");
        List<Company> companys = companyService.listAll(parameters, null);
        if (companys == null || companys.size() == 0) {
            return NONE_DATA_RETURN;
        }

        List<String> compayIds = companys.stream().map(Company::getId).collect(Collectors.toList());
        parameters = Lists.newArrayList();
        parameters.add(new Parameter("company_id", "in", compayIds));
        DepartmentService departmentService = (DepartmentService)SpringUtils.getBean("departmentService");
        List<Department> departments = departmentService.listAll(parameters, null);
        if (departments != null && departments.size() > 0) {
            return departments.stream().map(Department::getId).collect(Collectors.joining(","));
        }
        return NONE_DATA_RETURN;
    }

    private static String getDepartmentByMyCompanyAndSub() {
        User user = SessionUtils.getUser();
        Company myCompany = user.getCompany();

        // 下级公司
        List<Parameter> parameters = Lists.newArrayList();
        parameters.add(new Parameter("ids", "right_like", myCompany.getIds()));
        // parameters.add(new Parameter("id", "<>", myCompany.getId()));

        CompanyService companyService = (CompanyService)SpringUtils.getBean("companyService");
        List<Company> companys = companyService.listAll(parameters, null);
        if (companys == null || companys.size() == 0) {
            return NONE_DATA_RETURN;
        }

        List<String> compayIds = companys.stream().map(Company::getId).collect(Collectors.toList());
        parameters = Lists.newArrayList();
        parameters.add(new Parameter("company_id", "in", compayIds));
        DepartmentService departmentService = (DepartmentService)SpringUtils.getBean("departmentService");
        List<Department> departments = departmentService.listAll(parameters, null);
        if (departments != null && departments.size() > 0) {
            return departments.stream().map(Department::getId).collect(Collectors.joining(","));
        }
        return NONE_DATA_RETURN;
    }
}
